package cn.bdqn.fifthmedic.exception;


import cn.bdqn.fifthmedic.model.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Yuan
 * @create 2020-02-08 16:37
 */
@RestControllerAdvice   //异常处理类
@Slf4j  //记录日志
public class ExceptionHandle {
    @ExceptionHandler(AuthorizationException.class)
    public R authorizationException(AuthorizationException e){
        return R.error("没有授权");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public R authorizationException(IncorrectCredentialsException e){
        return R.error("用戶名或密碼不正確");
    }
    @ExceptionHandler(AuthenticationException.class)
    public R authenticationException(AuthenticationException e){
        return R.error("用戶名或密碼不正確");
    }
    @ExceptionHandler(UnknownAccountException.class)
    public R authorizationException(UnknownAccountException e){
        return R.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R authorizationException(Exception e){
        log.error(e.getMessage());  //打印日志
        e.printStackTrace();
        return R.error("系统错误,请与管理员联系");
    }
}
